package com.gdou.tlias.controller;

import com.gdou.tlias.pojo.Result;
import com.gdou.tlias.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    //上传本地磁盘
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传:{}{}{}", username, age, image);
//        //文件原始名
//        String name = image.getOriginalFilename();
//        //获取文件的后缀
//        int index=0;
//        if (name != null) {
//            index = name.lastIndexOf(".");
//        }
//        String endName = null;
//        if (name != null) {
//            endName = name.substring(index);
//        }
//        //将后缀名与UUID拼接
//        String newName= UUID.randomUUID()+endName;
//        image.transferTo(new File("D:\\code\\image\\" + newName));
//        return Result.success();
//    }

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名：{}",image.getOriginalFilename());
        //阿里云oss工具类
        Object url=aliOSSUtils.upload(image);
        log.info("文件上传完成url:{}",url);
        return Result.success(url);
    }


}
